#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 时间     : 2020/09/28 09:10
# 作者     : DL (584054432@qq.com)
# 网址     : https://gitee.com/dingliangPython/
# 软件版本 : Python3.7.3
# 功能     ：

class Base(object):
    """docstring for Base"""

    def __init__(self):
        super(Base, self).__init__()
        self.load_sql=self.loadSql
        self.exec_sql=self.execSql


    # 查询数据
    def loadSql(self, sql, result_list = False):
        try:
            conn = self.get_connect()
            cursor = conn.cursor()
            cursor.execute(sql)
            value = cursor.fetchall()
            data = []
            if value and len(value) >= 1:
                # 返回元组
                if not returnDict:
                    return value
                # 返回字典
                field = [d[0] for d in cursor.description]
                for v in value:
                    data.append(dict(zip(field, v)))
                conn.commit()
                cursor.close()
                conn.close()
                return data
            else:
                conn.commit()
                cursor.close()
                return 0
        except Exception as errorInfo:
            if "NoneType" in str(errorInfo):
                self.log.error(
                    "连接:{},主机:{},ODBC:{},库/实/或文件:{},连接失败,请检测连接".format(self.name, self.host, self.dsn, self.database))
                return
            self.log.error("查询出错:{},出错语句:{}".format(errorInfo, sql))

    # 执行 插入，更新，删除等操作语句
    def execSql(self, sql, value_list = []):
        cursor = self.connect.cursor()
        if not value_list:
            cursor.execute(sql)
            result = cursor.rowcount
            self.connect.commit()
            cursor.close()
            return result
        else:
            cursor.executemany(sql, value_list)
            result = cursor.rowcount
            self.connect.commit()
            cursor.close()
            return result

    # 获取占位符号
    def get_placeholder(self, field_list):
        if self._type == "Oracle":
            return [":{}".format(i) for i in enumerate(field_list)]
        if self._type in ["Sqlite", 'Access']:
            return ["?" for _ in field_list]
        if self._type in ("SQLServer", "Mysql"):
            return ["%s" for _ in field_list]

